package org.szy.plugin.dev.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentParser;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.noear.solon.annotation.ProxyComponent;
import org.noear.solon.core.handle.Context;
import org.szy.core.common.page.QueryPage;
import org.szy.core.common.service.impl.BaseServiceImpl;
import org.szy.core.common.uilts.IpAddressUtil;
import org.szy.core.common.uilts.IpUtils;
import org.szy.core.common.uilts.ReqUtil;
import org.szy.plugin.dev.entity.DevLoginLog;
import org.szy.plugin.dev.mapper.DevLoginLogMapper;
import org.szy.plugin.dev.param.DevLoginLogQueryParam;
import org.szy.plugin.dev.service.DevLoginLogService;

import static org.szy.plugin.dev.entity.table.DevLoginLogTableDef.DEV_LOGIN_LOG;

/**
 * 登录日志Service接口实现类
 *
 * @author wangbuhao
 * @since 2023-06-28 16:05:55
 **/
@ProxyComponent
public class DevLoginLogServiceImpl extends BaseServiceImpl<DevLoginLogMapper, DevLoginLog> implements DevLoginLogService {

    @Override
    public QueryWrapper qwByQuery(DevLoginLogQueryParam param) {
        QueryWrapper qw = new QueryWrapper();
        if (StrUtil.isNotEmpty(param.getColumnField())) {
            qw.select(param.getColumnField().split(","));
        }

        if (StrUtil.isNotEmpty(param.getType())) qw.and(DEV_LOGIN_LOG.TYPE.eq(param.getType()));
        if (StrUtil.isNotEmpty(param.getTypeLike())) qw.and(DEV_LOGIN_LOG.TYPE.like(param.getTypeLike()));
        if (StrUtil.isNotEmpty(param.getAccount())) qw.and(DEV_LOGIN_LOG.ACCOUNT.eq(param.getAccount()));
        if (StrUtil.isNotEmpty(param.getAccountLike())) qw.and(DEV_LOGIN_LOG.ACCOUNT.like(param.getAccountLike()));
        if (StrUtil.isNotEmpty(param.getUserId())) qw.and(DEV_LOGIN_LOG.USER_ID.eq(param.getUserId()));
        if (ObjectUtil.isNotEmpty(param.getStartTime())) qw.and(DEV_LOGIN_LOG.TIME.ge(param.getStartTime()));
        if (ObjectUtil.isNotEmpty(param.getEndTime())) qw.and(DEV_LOGIN_LOG.TIME.le(param.getEndTime()));
        if (param.getStatus() != null) qw.and(DEV_LOGIN_LOG.STATUS.eq(param.getStatus()));
        qw.orderBy(DEV_LOGIN_LOG.TIME.desc());
        return qw;
    }

    @Override
    public Page<DevLoginLog> page(QueryPage<DevLoginLog> page, DevLoginLogQueryParam param) {
        QueryWrapper queryWrapper = qwByQuery(param);

        return this.page(page, queryWrapper);
    }

    @Override
    public DevLoginLog add(String type, Long userId, String account, Integer status, String msg) {
        DevLoginLog loginLog = new DevLoginLog(type, userId, account, status, msg);
        Context current = Context.current();
        String ip = IpUtils.getIp(current);
        String address = IpAddressUtil.getCityInfo(ip);
        loginLog.setClientIp(ip);
        loginLog.setLocation(address);
        UserAgent userAgent = ReqUtil.getUserAgent(current);
        loginLog.setBrowser(userAgent.getBrowser().getName());
        loginLog.setOs(userAgent.getOs().getName());
        return save(loginLog) ? loginLog : null;
    }
}
